package com.csst.business.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.baidu.inf.iis.bcs.BaiduBCS;
import com.baidu.inf.iis.bcs.auth.BCSCredentials;
import com.csst.business.model.TPhotoGroup;
import com.csst.business.model.TPhotos;
import com.csst.business.service.IPhotoService;
import com.csst.business.util.PageData;
import com.csst.business.util.PaginationUtil;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class PhotoAction extends ActionSupport{
	private IPhotoService photoService;
	private TPhotoGroup photog;
	private List<TPhotoGroup> photogList;
	private TPhotos photo;
	private List<TPhotos> photoList;
	private File uploadfile;
	private String uploadfileFileName;
	//分页参数
	private PageData pageData;
	private int pagesize=10;
	private  int currentPage = 1;
	public String testAction(){
		return SUCCESS;
	}
	
	public String test(){
		//PhotoService.saveDate(Photo);
		return "aaa";
	}
	
	public String getPhotoGroupList(){
		int totle = photoService.getOrderTotle();
		pageData= PaginationUtil.getPageData(pagesize,totle, currentPage);
		photogList =  photoService.getPhotoGroupList(pageData.getStartNum(),pageData.getEndNum());
		return "photoglist";
	}
	public String getWPhotoGroupList(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String openId = (String) request.getSession().getAttribute("openId");
		if(openId==null){
			openId = (String)request.getParameter("openId");
			request.getSession().setAttribute("openId", openId);
		}
		int totle = photoService.getOrderTotle();
		pageData= PaginationUtil.getPageData(pagesize,totle, currentPage);
		photogList =  photoService.getPhotoGroupList(pageData.getStartNum(),pageData.getEndNum());
		return "Wphotoglist";
	}
	
	public String savePhotoGroup(){
		photog.setGCreatetime(new Date());
		photog.setGCreateuser("管理员");
		photoService.saveGroup(photog);
		return "photog";
	}
	
	public String updatePhotoGroup(){
		photoService.updateGroup(photog);
		return "photog";
	}
	
	public String getPhotoGById(){
		photog = photoService.getPhotoGById(photog.getId());
		return "photogPre";
	}
	public String addPhotoPre(){
		photog = photoService.getPhotoGById(photog.getId());
		photo = new TPhotos();
		photo.setTPhotoGroup(photog);
		return "photoPre";
	}
	
	public String savePhoto(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String oldFileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+this.uploadfileFileName.substring(this.uploadfileFileName.indexOf("."));
		String HOST = "bcs.duapp.com";  
	    String AK = "n2S13HjrMa6pRm07qznBwjyu";           
	    String SK = "We42IiC51GxxjSIuHUMEzi0ooW0cojWV"; 
	    BCSCredentials credentials = new BCSCredentials(AK, SK);
		BaiduBCS bbcs = new BaiduBCS(credentials, HOST);
	    String  bucketName ="gongfumeng";
	    String saveFileName="";
	    String imgurl="";
		try {
			saveFileName = new String(("/upload/photo/"  + oldFileName).getBytes(),"UTF-8");
			bbcs.putObject(bucketName, saveFileName, uploadfile); 
		    imgurl = "http://bcs.duapp.com/gongfumeng/"+ saveFileName;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		photo.setPName(saveFileName);
		photo.setPUpdate(new Date());
		photo.setPUpuser("管理员");
		photoService.savePhoto(photo);
		return "photog";
	}
	
	
	public void  uploadPic(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		//uploadfile
		//String oldFileName = uploadfile.getName();
		String oldFileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+this.uploadfileFileName.substring(this.uploadfileFileName.indexOf("."));
	    String fileExt = this.uploadfileFileName.substring(this.uploadfileFileName.indexOf("."));  
	    if (!fileExt.equalsIgnoreCase("jpg") && !fileExt.equalsIgnoreCase("jpeg")  && fileExt.equalsIgnoreCase("png")){  //标记1  
	        response.setContentType("application/json");
	        String imgurl = "http://bcs.duapp.com/mybucket/image/error_upload.jpg" ; 
	        String json = "{\"status\":\"fail\", \"imgurl\":\"" + imgurl + "\"}";  //标记2  
	        PrintWriter out;
			try {
				out = response.getWriter();
				 out.print(json);  
			        out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
	       
	    }
	    String HOST = "bcs.duapp.com";  
	    String AK = "n2S13HjrMa6pRm07qznBwjyu";           
	    String SK = "We42IiC51GxxjSIuHUMEzi0ooW0cojWV"; 
	    BCSCredentials credentials = new BCSCredentials(AK, SK);
		BaiduBCS bbcs = new BaiduBCS(credentials, HOST);
	    String  bucketName ="gongfumeng";
	    String saveFileName;
		try {
			saveFileName = new String(("/upload/photo/"  + oldFileName).getBytes(),"UTF-8");
			bbcs.putObject(bucketName, saveFileName, uploadfile);
			//删除图骗
			/*String object = "/upload/photo/20140814222917.jpg";
			bbcs.deleteObject(bucketName, object);*/
		    response.setContentType("application/json");
		    String imgurl = "http://bcs.duapp.com/gongfumeng/"+ saveFileName;  
		    String json = "{\"status\":\"success\", \"imgurl\":\"" + imgurl + "\"}";  
		    PrintWriter out;
			try {
				out = response.getWriter();
				out.print(json);  
			    out.close(); 
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
	       
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    /*String saveFileData = file.read();*/
	  
	   
	}
	
	public String lookPhotoPre(){
		
		int totle = photoService.getPhotoTotle(photog.getId());
		pageData= PaginationUtil.getPageData(pagesize,totle, currentPage);
		photoList =  photoService.getPhotosBypgId(photog.getId(),pageData.getStartNum(),pageData.getEndNum());
		return "photolook";
	}
	public String lookWPhotoPre(){
		
		int totle = photoService.getPhotoTotle(photog.getId());
		pageData= PaginationUtil.getPageData(pagesize,totle, currentPage);
		photoList =  photoService.getPhotosBypgId(photog.getId(),1,totle);
		return "Wphotolook";
	}
	
	public boolean hasChildPhoto(String gid){
		boolean ff = photoService.hasChildPhoto(gid);
		return ff;
	}
	
	public boolean deletePhotoGroup(String gid){
		photoService.delteGroup(gid);
		return true;
	}
	public boolean deletePhoto(String gid){
		photo = photoService.getPhotoById(gid);
		String photoName = photo.getPName();
		String HOST = "bcs.duapp.com";  
	    String AK = "n2S13HjrMa6pRm07qznBwjyu";           
	    String SK = "We42IiC51GxxjSIuHUMEzi0ooW0cojWV"; 
	    BCSCredentials credentials = new BCSCredentials(AK, SK);
		BaiduBCS bbcs = new BaiduBCS(credentials, HOST);
	    String  bucketName ="gongfumeng";
	    try {
	    	bbcs.deleteObject(bucketName, photoName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		photoService.deltephoto(gid);
		return true;
	}
	
	
	public IPhotoService getPhotoService() {
		return photoService;
	}

	public void setPhotoService(IPhotoService photoService) {
		this.photoService = photoService;
	}

	public TPhotoGroup getPhotog() {
		return photog;
	}

	public void setPhotog(TPhotoGroup photog) {
		this.photog = photog;
	}

	public List<TPhotoGroup> getPhotogList() {
		return photogList;
	}

	public void setPhotogList(List<TPhotoGroup> photogList) {
		this.photogList = photogList;
	}

	public TPhotos getPhoto() {
		return photo;
	}

	public void setPhoto(TPhotos photo) {
		this.photo = photo;
	}

	public List<TPhotos> getPhotoList() {
		return photoList;
	}

	public void setPhotoList(List<TPhotos> photoList) {
		this.photoList = photoList;
	}

	public PageData getPageData() {
		return pageData;
	}

	public void setPageData(PageData pageData) {
		this.pageData = pageData;
	}

	public int getPagesize() {
		return pagesize;
	}

	public void setPagesize(int pagesize) {
		this.pagesize = pagesize;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public File getUploadfile() {
		return uploadfile;
	}

	public void setUploadfile(File uploadfile) {
		this.uploadfile = uploadfile;
	}

	public String getUploadfileFileName() {
		return uploadfileFileName;
	}

	public void setUploadfileFileName(String uploadfileFileName) {
		this.uploadfileFileName = uploadfileFileName;
	}
	
	
}
